<?php

/**
 * Cart short summary.
 *
 * Cart description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Cart extends Shop_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Goods_cart_model');
        $this->load->Model('Goods_inventory_model');
    }

    public function addcart()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $customer_id=$this->get_customer_id();
            $this->Goods_cart_model->customer_id=$customer_id;
            $this->Goods_cart_model->goods_id=$this->input->post('goodsid');
            $this->Goods_cart_model->color_id=$this->input->post('clolerid');
            $this->Goods_cart_model->size_id=$this->input->post('sizeid');
            $this->Goods_cart_model->count=$this->input->post('goods_count');
            $goodscart_model=$this->Goods_cart_model;
            $inventory_count=$this->Goods_inventory_model->get_goods_inventory_count($goodscart_model);
            if(!empty($inventory_count) && $inventory_count->inventory_count>0)
            {
                if($inventory_count->inventory_count< $this->Goods_cart_model->count)
                {
                    $this->ajaxmsgmodel->Msg='库存没有这么多商品，请减少该商品的数量';
                    $this->ajaxmsgmodel->Statu='err';
                }
                else
                {
                    $res= $this->Goods_cart_model->add_cart($goodscart_model);
                    if($res)
                    {

                        $this->ajaxmsgmodel->Msg='已放入购物车';
                        $this->ajaxmsgmodel->Statu='ok';

                    }
                    else
                    {
                        $this->ajaxmsgmodel->Msg='添加购物车失败';
                        $this->ajaxmsgmodel->Statu='err';
                    }
                }
            }
            else
            {
                $this->ajaxmsgmodel->Msg='该商品库存告罄，请选择其他商品';
                $this->ajaxmsgmodel->Statu='err';
            }

        } else{
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //来至商城首页——加入购物车
    public function add_cart_from_shop()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $customer_id=$this->get_customer_id();
            $this->Goods_cart_model->customer_id=$customer_id;
            $goodsid=$this->input->post('goodsid');
            //重库存中查询第一件商品添加到购物车
            $this->load->Model('Goods_inventory_model');
            $goods_inventory_model=$this->Goods_inventory_model->get_first_goods_by_goods_id($goodsid);
            if(empty($goods_inventory_model) || count($goods_inventory_model)<1 ||$goods_inventory_model->inventory_count<1)
            {
                $this->ajaxmsgmodel->Msg='该商品库存告罄，请选择其他商品';
                $this->ajaxmsgmodel->Statu='err';
            }
            else
            {
                $this->Goods_cart_model->customer_id=$customer_id;
                $this->Goods_cart_model->goods_id=$goodsid;
                $this->Goods_cart_model->color_id=$goods_inventory_model->goods_color_id;
                $this->Goods_cart_model->size_id=$goods_inventory_model->goods_size_id;
                $this->Goods_cart_model->count=1;
                $goodscart_model=$this->Goods_cart_model;
                $res= $this->Goods_cart_model->add_cart($goodscart_model);
                if($res)
                {

                    $this->ajaxmsgmodel->Msg='已放入购物车';
                    $this->ajaxmsgmodel->Statu='ok';

                }
                else
                {
                    $this->ajaxmsgmodel->Msg='添加购物车失败';
                    $this->ajaxmsgmodel->Statu='err';
                }
            }
        }else{
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');
        }
        echo(json_encode($this->ajaxmsgmodel));

    }

    public function get_cart_after_login()
    {
        $customer_id=$this->get_customer_id();
        $res= $this->Goods_cart_model->get_goods_cart_count($customer_id);
        if(!empty($res)){
            $car_count_html='get_cartlist()|'.'<span onclick="get_cartlist()">'.$res->cart_count.'</span>';
        }
        else{
            $car_count_html='';
        }
        echo $car_count_html;
    }

    public function cartlist()
    {
        $this->frame_check_redirect();
        $customer_id=$this->get_customer_id();
        $data['goodscarts']=$this->Goods_cart_model->get_goods_carts($customer_id);
        $this->load->view('Pre/cart/cartlist.html',$data);
    }

    public function del_cart()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $cartid=$this->input->post('cartid');
            $backurl=$this->input->post('backurl');
            $res=$this->Goods_cart_model->del_cart($cartid);
            if($res)
            {

                $this->ajaxmsgmodel->Msg='删除购物车成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$backurl;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='删除购物车失败';
                $this->ajaxmsgmodel->Statu='err';
            }

        } else{
            $this->ajaxmsgmodel->Statu='f_okto';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');;
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function update_cart_count()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $cartid=$this->input->post('cartid');
            $new_val=$this->input->post('new_val');
            $goodscart_model=$this->Goods_cart_model->get_cart_by_id($cartid);
            $inventory_count=$this->Goods_inventory_model->get_goods_inventory_count($goodscart_model);
            if($inventory_count->inventory_count<$new_val)
            {
                $this->ajaxmsgmodel->Msg='库存没有这么多商品，请减少该商品的数量';
                $this->ajaxmsgmodel->Statu='err';
            }
            else
            {
                $res=$this->Goods_cart_model->update_cart_count($cartid,$new_val);
                if($res)
                {

                    $this->ajaxmsgmodel->Msg='更新购物车成功';
                    $this->ajaxmsgmodel->Statu='ok';
                }
                else
                {
                    $this->ajaxmsgmodel->Msg='更新购物车失败';
                    $this->ajaxmsgmodel->Statu='err';
                }
            }


        } else{
            $this->ajaxmsgmodel->Statu='f_okto';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}